import { isArray, isObject } from '@/utils/types'
import Vue from 'vue'
import i18n from '@/i18n/i18n'

export default {
  methods: {
    /**
     * @description: 通过高级筛选type获取条件名称
     * @param {*} type
     * @return {*}
     */
    getAdvancedFilterOptionName(type, formType) {
      const isDate = formType === 'date' || formType === 'datetime'
      return (
        {
          1: i18n.t('common.AdvancedFilter.371ecb542ba2b445f3ff9684c7d24c2c'),
          2: i18n.t('common.AdvancedFilter.581a5e3a46fa33151b59a93cbc323a5a'),
          3: i18n.t('common.AdvancedFilter.a69bd2918764561a945568317586f438'),
          4: i18n.t('common.AdvancedFilter.d58a021a2f4ab2256252f9fdea910ce3'),
          5: i18n.t('common.AdvancedFilter.eb962e7bd543583981e45c7853cf18ba'),
          6: i18n.t('common.AdvancedFilter.787f060bce6b783bffa715ef003e1f73'),
          7: isDate
            ? i18n.t('common.AdvancedFilter.f1be05ca443d2d3922d4e66a99b6ecea')
            : i18n.t('common.AdvancedFilter.5ccc95cacb050f0cd21f00bfb5995131'),
          8: isDate
            ? i18n.t('common.AdvancedFilter.c6feab25ea4fcc5ef1257c890e843a3b')
            : i18n.t('common.AdvancedFilter.01bac542fe2a69840117f37f5e7d37b0'),
          9: isDate
            ? i18n.t('common.AdvancedFilter.951b9e7a1eea0be3ed1480a380ab1f71')
            : i18n.t('common.AdvancedFilter.a2aae311cbf0ea082345f7d6fe8f8f17'),
          10: isDate
            ? i18n.t('common.AdvancedFilter.9400814457fefeaeffa0310621657817')
            : i18n.t('common.AdvancedFilter.f48f46986408231d162f1cc99470d4a4'),
          12: i18n.t('common.AdvancedFilter.70ed05b453e14c009fb258990b7fcdc8'),
          13: i18n.t('common.AdvancedFilter.966f47fbd4ce50323bc7d23e4fc0954c'),
          14: isDate
            ? i18n.t('common.AdvancedFilter.171491a3069145ab789df7d80f845c71')
            : i18n.t('common.AdvancedFilter.df4587e772aaaf5f0db089d5c5245ce8')
        }[type] || ''
      )
    },

    /**
     * 根据类型获取条件
     * @param {String} formType
     * @param {String} fieldName // 后期改为仅以formType 作为依据，外层进行formType校准
     */
    getAdvancedFilterOptions(formType, fieldName) {
      // 单行文本、多行文本、网址、手机、邮箱
      // 等于、不等于、包含、不包含、开始于、结束于、为空、不为空
      if (
        formType == 'text' ||
        formType == 'textarea' ||
        formType == 'website' ||
        formType == 'mobile' ||
        formType == 'email' ||
        formType == 'module' ||
        formType == 'serial_number'
      ) {
        return [
          {
            value: 'is',
            label: i18n.t(
              'common.AdvancedFilter.bc1e67d5d11a9738c0f11145493be3a1'
            ),
            disabled: false,
            type: 1
          },
          {
            value: 'isNot',
            label: i18n.t(
              'common.AdvancedFilter.ec8dc2b8e938b1445b43026fe7e29fd8'
            ),
            disabled: false,
            type: 2
          },
          {
            value: 'contains',
            label: i18n.t(
              'common.AdvancedFilter.72d5d0c75feca8ffde1ea65eef5a43be'
            ),
            disabled: false,
            type: 3
          },
          {
            value: 'notContains',
            label: i18n.t(
              'common.AdvancedFilter.2f391bd3b1cc4941af285b2c9c10705f'
            ),
            disabled: false,
            type: 4
          },
          {
            value: 'startWith',
            label: i18n.t(
              'common.AdvancedFilter.83df087036e1fcfcf9610eb4b7ab75ab'
            ),
            disabled: false,
            type: 12
          },
          {
            value: 'endWith',
            label: i18n.t(
              'common.AdvancedFilter.0f9cfd45c645d62a54e0c972e8a3f0dd'
            ),
            disabled: false,
            type: 13
          },
          {
            value: 'isNull',
            label: i18n.t(
              'common.AdvancedFilter.0d8179171003666cb4cbc9300b88ec99'
            ),
            disabled: false,
            type: 5
          },
          {
            value: 'isNotNull',
            label: i18n.t(
              'common.AdvancedFilter.1ef37aa8fef48bd5b7b3065b19ea8f53'
            ),
            disabled: false,
            type: 6
          }
        ]
      } else if (
        formType === 'invoiceStatus' ||
        formType == 'checkStatus' ||
        formType == 'dealStatus' ||
        formType == 'receivedStatus' ||
        formType == 'status' || // this.crmType === 'product' && fieldName === 'status' && formType === 'select' 通过conditionTypeFun 在外层校准为 status
        (formType == 'select' && fieldName == 'isWxUser')
      ) {
        return [
          {
            value: 'is',
            label: i18n.t(
              'common.AdvancedFilter.3f2962995c7b674eb2587fbec6bef326'
            ),
            disabled: false,
            type: 1
          },
          {
            value: 'isNot',
            label: i18n.t(
              'common.AdvancedFilter.be4f936229344a19b8a1c5cfc48cdf7d'
            ),
            disabled: false,
            type: 2
          }
        ]
      } else if (
        // 下拉框 等于、不等于、为空、不为空
        formType == 'select' ||
        formType == 'field_attention'
      ) {
        return [
          {
            value: 'is',
            label: i18n.t(
              'common.AdvancedFilter.b00f1fbffdb5e53c1f2e8c681a0c10ca'
            ),
            disabled: false,
            type: 1
          },
          {
            value: 'isNot',
            label: i18n.t(
              'common.AdvancedFilter.76beaded3d672d414bc4ef84071912a7'
            ),
            disabled: false,
            type: 2
          },
          {
            value: 'isNull',
            label: i18n.t(
              'common.AdvancedFilter.cb7ffba7e740116b5ec035865497718b'
            ),
            disabled: false,
            type: 5
          },
          {
            value: 'isNotNull',
            label: i18n.t(
              'common.AdvancedFilter.d05fdf1d74ec78be85449bbbcb889ef9'
            ),
            disabled: false,
            type: 6
          }
        ]
      } else if (
        // 布尔值 等于、不等于
        formType == 'boolean_value'
      ) {
        return [
          {
            value: 'is',
            label: i18n.t(
              'common.AdvancedFilter.7a3c0af0c29f214e96f8bb1d61fd27ba'
            ),
            disabled: false,
            type: 1
          },
          {
            value: 'isNot',
            label: i18n.t(
              'common.AdvancedFilter.865964e9e4b11bdceea09da20fc9c1a0'
            ),
            disabled: false,
            type: 2
          }
        ]
      } else if (formType == 'checkbox' || formType == 'location') {
        return [
          {
            value: 'is',
            label: i18n.t(
              'common.AdvancedFilter.57b0950e32d1612ed55118fd89141377'
            ),
            disabled: false,
            type: 1
          },
          {
            value: 'isNot',
            label: i18n.t(
              'common.AdvancedFilter.05eaeded36bcc524b173d533466057f3'
            ),
            disabled: false,
            type: 2
          },
          {
            value: 'contains',
            label: i18n.t(
              'common.AdvancedFilter.631d16d7b2a6e7a6f225712dc51af6dc'
            ),
            disabled: false,
            type: 3
          },
          {
            value: 'notContains',
            label: i18n.t(
              'common.AdvancedFilter.386a424d0d07e4cb44af9f9ff78683b4'
            ),
            disabled: false,
            type: 4
          },
          {
            value: 'isNull',
            label: i18n.t(
              'common.AdvancedFilter.58bd2f582387ec2ba1c4472fe4c7a63a'
            ),
            disabled: false,
            type: 5
          },
          {
            value: 'isNotNull',
            label: i18n.t(
              'common.AdvancedFilter.28206464a05fcd6d65a41250307f19ea'
            ),
            disabled: false,
            type: 6
          }
        ]
        // 数字、货币、百分数
        // 等于、不等于、大于、大于等于、小于、小于等于、区间（范围）、为空、不为空
      } else if (
        formType == 'number' ||
        formType == 'floatnumber' ||
        formType == 'percent'
      ) {
        return [
          {
            value: 'is',
            label: i18n.t(
              'common.AdvancedFilter.ae594d69a7286929aace489ab2eccd28'
            ),
            disabled: false,
            type: 1
          },
          {
            value: 'isNot',
            label: i18n.t(
              'common.AdvancedFilter.eddbb9be59ec6a90f83954d51200a633'
            ),
            disabled: false,
            type: 2
          },
          {
            value: 'gt',
            label: i18n.t(
              'common.AdvancedFilter.72f558a01d9a853ff9925a3a3c69f3ec'
            ),
            disabled: false,
            type: 7
          },
          {
            value: 'egt',
            label: i18n.t(
              'common.AdvancedFilter.4b5fa8178c387b64a0b662f0b7921631'
            ),
            disabled: false,
            type: 8
          },
          {
            value: 'lt',
            label: i18n.t(
              'common.AdvancedFilter.50bf84d1d109363d6416311b69f58b83'
            ),
            disabled: false,
            type: 9
          },
          {
            value: 'elt',
            label: i18n.t(
              'common.AdvancedFilter.498b8454bc24e3e1ac250c6b41baa4be'
            ),
            disabled: false,
            type: 10
          },
          {
            value: 'range',
            label: i18n.t(
              'common.AdvancedFilter.28363ddf0e041ec5a1194b34869e0f2d'
            ),
            disabled: false,
            type: 14
          },
          {
            value: 'isNull',
            label: i18n.t(
              'common.AdvancedFilter.6a4a80e37bdb1b9fd2f0d9f33afa7ccc'
            ),
            disabled: false,
            type: 5
          },
          {
            value: 'isNotNull',
            label: i18n.t(
              'common.AdvancedFilter.3fe33baf78b53a56dc61c379f2bf2830'
            ),
            disabled: false,
            type: 6
          }
        ]
        // 日期、日期时间
        // 等于、不等于、早于(大于)、晚于（小于）、不早于（小于等于）、不晚于（大于等于）、等于（时间段）、为空、不为空
      } else if (formType == 'date' || formType == 'datetime') {
        return [
          {
            value: 'is',
            label: i18n.t(
              'common.AdvancedFilter.6c8927f03e7c00284daf42bf6120f0c4'
            ),
            disabled: false,
            type: 1
          },
          {
            value: 'isNot',
            label: i18n.t(
              'common.AdvancedFilter.f1942aad1dee9271520a08243b5ad613'
            ),
            disabled: false,
            type: 2
          },
          {
            value: 'lt',
            label: i18n.t(
              'common.AdvancedFilter.939615ddc3ccd0f3877e42b614961ec0'
            ),
            disabled: false,
            type: 9
          },
          {
            value: 'gt',
            label: i18n.t(
              'common.AdvancedFilter.bc439c9b5b3aabd7fcabafd7b1986dc6'
            ),
            disabled: false,
            type: 7
          },
          {
            value: 'egt',
            label: i18n.t(
              'common.AdvancedFilter.9c9b8afc89644ba88ca2938da1a38c07'
            ),
            disabled: false,
            type: 8
          },
          {
            value: 'elt',
            label: i18n.t(
              'common.AdvancedFilter.eb8fde51d9f3f6363d873cc6bf658927'
            ),
            disabled: false,
            type: 10
          },
          {
            value: 'range',
            label: i18n.t(
              'common.AdvancedFilter.a0f6fb95653960964f55443edb9c3fa2'
            ),
            disabled: false,
            type: 14
          },
          {
            value: 'isNull',
            label: i18n.t(
              'common.AdvancedFilter.c8a23c0b412ae003b22cfc736a671c8c'
            ),
            disabled: false,
            type: 5
          },
          {
            value: 'isNotNull',
            label: i18n.t(
              'common.AdvancedFilter.b6b3ea2d056500b9fb55ead462f2e888'
            ),
            disabled: false,
            type: 6
          }
        ]
        // 日期区间 地址 无判断符
      } else if (formType == 'date_interval' || formType == 'position') {
        return []
        // 人员、部门 包含、不包含、为空、不为空
      } else if (
        [
          'user',
          'structure',
          'single_user',
          'field_tag'
        ].includes(formType)
      ) {
        return [
          {
            value: 'contains',
            label: i18n.t(
              'common.AdvancedFilter.0aea5f22826c69145549b05b81f1571d'
            ),
            disabled: false,
            type: 3
          },
          {
            value: 'notContains',
            label: i18n.t(
              'common.AdvancedFilter.a216ffa8e6e1b817bd81c934c3291a09'
            ),
            disabled: false,
            type: 4
          },
          {
            value: 'isNull',
            label: i18n.t(
              'common.AdvancedFilter.38236d5d7ceaf1a20d20cc9f57118bd4'
            ),
            disabled: false,
            type: 5
          },
          {
            value: 'isNotNull',
            label: i18n.t(
              'common.AdvancedFilter.38df6e87258fae86d69d62ae48a38157'
            ),
            disabled: false,
            type: 6
          }
        ]
      } else {
        return []
      }
    },

    /**
     * 根据类型确定不同的默认值属性
     * item 校准后对象
     * obj 字段对象
     * formTypeFun 校准formType 部分页面（产品下状态）formType 需要校准
     */
    getAdvancedFilterDefaultItemByFormType(item, obj, formTypeFun) {
      if (!obj) {
        obj = item
      }

      if (['flowGroup', 'business_cause'].includes(item.formType)) {
        item.typeOption = obj.setting
        item.settingList = []
        Vue.set(item, 'flowName', '')
        Vue.set(item, 'settingName', '')
        item.value = ''
      } else if (
        item.formType == 'checkStatus' ||
        item.formType == 'dealStatus' ||
        item.formType == 'invoiceStatus' ||
        item.formType == 'receivedStatus' ||
        item.formType == 'status'
      ) {
        item.setting = obj.setting || []
        item.value = ''
      } else if (item.formType == 'map_address') {
        item.address = {
          state: '',
          city: '',
          area: ''
        }
      } else if (
        item.formType === 'user' ||
        item.formType === 'single_user' ||
        item.formType === 'structure' ||
        item.formType === 'category' ||
        item.formType === 'position'
      ) {
        item.value = []
      } else if (item.formType === 'date' || item.formType === 'datetime') {
        item.value = '' // 时间点
        Vue.set(item, 'timeType', '')
        Vue.set(item, 'timeTypeName', '')
        Vue.set(item, 'range', [])
      } else if (
        item.formType == 'select' ||
        item.formType === 'checkbox' ||
        item.formType === 'field_attention'
      ) {
        item.setting = obj.setting || []
        item.value = []
      } else if (
        item.formType == 'number' ||
        item.formType == 'floatnumber' ||
        item.formType == 'percent'
      ) {
        item.min = ''
        item.max = ''
        item.value = ''
      } else if (item.formType === 'field_tag') {
        item.setting = obj.setting || []
        item.value = ''
      } else {
        item.value = ''
      }

      const conditions = this.getAdvancedFilterOptions(
        formTypeFun ? formTypeFun(item) : item.formType,
        item.fieldName
      )
      if (conditions.length > 0) {
        const conditionObj = conditions[0]
        item.condition = conditionObj.value
        item.type = conditionObj.type
      } else {
        item.condition = 'is'
        item.type = 1
      }
      return item
    },

    /**
     * 重置高级筛选指
     */
    resetAdvancedFilterFieldsValue(list) {
      list.forEach((item) => {
        if (
          item.formType == 'number' ||
          item.formType == 'floatnumber' ||
          item.formType == 'percent'
        ) {
          item.min = ''
          item.max = ''
          item.value = ''
        } else if (item.formType == 'business_cause') {
          item.flowName = ''
          item.settingName = ''
          item.value = ''
          item.settingList = []
        } else if (item.formType == 'map_address') {
          item.address = {
            state: '',
            city: '',
            area: ''
          }
        } else if (['date', 'datetime'].includes(item.formType)) {
          item.value = ''
          item.range = []
        } else if (isArray(item.value)) {
          item.value = []
        } else if (isObject(item.value)) {
          item.value = {}
        } else {
          item.value = ''
        }
      })
    }
  }
}
